<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use hg\apidoc\annotation as Doc;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

/**
 * App\Models\Coupon
 *
 * @property int $id
 * @property int $mode 类型:0=洗车优惠券,1=商品优惠券
 * @property string $title 优惠券标题
 * @property int $range 使用限制:0=全场通用1=指定分类,2=指定服务商品
 * @property string $full_price 满足使用条件
 * @property string $price 优化金额
 * @property int $days 领取后有效天数时长(天)
 * @property string $limit_time 到期时间
 * @property int $shop_id 店铺ID
 * @property int $cid 分类ID
 * @property int $good_id 商品ID
 * @property int $service_id 服务ID
 * @property array|null $areas 所属地区ID组,默认null表示全国地区显示
 * @property int $status 状态:0=正常,1=隐藏
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon query()
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereAreas($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereCid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereDays($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereFullPrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereGoodId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereMode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereRange($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereServiceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereShopId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereUpdatedAt($value)
 * @mixin \Eloquent
 * @method static \Illuminate\Database\Eloquent\Builder|Coupon whereLimitTime($value)
 */
class Coupon extends Model
{
	use HasDateTimeFormatter;

    protected $casts = ['full_price'=>'float','price'=>'float'];

    protected static function booted()
    {
        self::saving(function (Coupon $coupon){
            if ($coupon->range==0){
//                $coupon->shop_id = 0;
                $coupon->cid = 0;
                $coupon->service_id = 0;
            }elseif ($coupon->range==1){
//                $coupon->shop_id = 0;
                $coupon->service_id = 0;
            }elseif ($coupon->range==2){
//                $coupon->shop_id = 0;
                $service =  Service::find($coupon->service_id);
                if ($service){
                    $coupon->cid =$service->cid;
                }else{
                    $coupon->cid = 0;
                }
            }
        });
    }

    public static function getRanges(): array
    {
        return [0=>'全场通用',1=>'指定分类',2=>'指定服务'];
    }

    /**
     * 获取优惠券列表
     * @param int $user_id
     * @return array
     */
    public static function getList(int $user_id = 0): array
    {
        $pageSize = request()->input("pageSize",10);
        $ids = UserCoupon::query()->where("user_id",$user_id)->pluck("coupon_id")->toArray();
        $where = function ($query) use ($ids) {
            $map = ['status' => 0, 'mode' => 0];
            $shop_id = request()->input("shop_id",0);
            $query->when($shop_id,function ($q, $value){
                $q->where("shop_id",$value);
            })->where($map)->whereDate("limit_time",">=",now())->when($ids,function ($query,$value){
                $query->whereNotIn("id",$value);
            });
        };
        $where1 = function ($query) use ($where) {
            $shop_id = request()->input("shop_id",0);
            $query->when($shop_id,function ($q, $value){
                $q->where("shop_id",$value);
            });
        };
        $list = self::query()
            ->select(['id','shop_id','title','full_price','price','range','cid','service_id','limit_time'])
            ->where($where)
            ->orWhere($where1)
            ->orderByDesc("id")
            ->paginate($pageSize);
        foreach ($list as $item){
            $item->full_price = floatval($item->full_price);
            $item->price = floatval($item->price);
            $item->limit_time = Carbon::parse($item->limit_time)->format("Y.m.d");
            $shop = ShopUser::query()->where("id",$item->shop_id)->first();
            $item->text = $shop?"仅限【{$shop->name}】使用":"";
        }
        $data['items'] = $list->items();
        $data['lastPage'] = $list->lastPage();
        $data['total'] = $list->total();
        return $data;
    }
}
